package com.smart.stock.module.maindata.services;

import com.smart.stock.module.maindata.entity.StockPriceDaily;
import com.smart.stock.common.util.h2.H2Util;
import com.smart.stock.module.spider.util.TradingDateUtil;
import org.bson.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.BasicQuery;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.sql.ResultSet;
import java.util.List;

/**
 * Stock service
 *
 * @author chenguoxiang
 * @create 2018-03-27 10:27
 **/
@Service
public class BigScreenService {//todo

    @Autowired
    private MongoTemplate template;

    public String getIndexSH() {
        return null;
    }

    public String getIndexSZ() {
        return null;
    }

    public String getIndexKCB() {
        return null;
    }

    public String getIndexCYB() {
        return null;
    }

    public String getIndexTengLuoXian() throws Exception {
        List<String> days = TradingDateUtil.getDayBounds(1,0,0);
        //select * from x where xx>=x and xx<x
        Document dbObject = new Document();
        Document fieldObject = new Document();
        Query query = new BasicQuery(dbObject, fieldObject);
        query.addCriteria(Criteria.where("tradeDate").gte(Integer.valueOf(days.get(0))).lt(Integer.valueOf(days.get(1))));
        List<StockPriceDaily> list = template.find(query, StockPriceDaily.class);
        System.out.println(list);

        H2Util<StockPriceDaily> h2Util = new H2Util<StockPriceDaily>();
        String table = h2Util.createH2TableFromList(list);
        ResultSet rs = H2Util.execQuerySQL("select min(close) min from " + table);
        while (rs.next()){
            Object object = rs.getObject(1);
            System.out.println(object);
        }
        return null;
    }
}
